package feifei.cookiezuoye;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/servlet2.action")
public class Servlet2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        username= new String(username.getBytes("iso8859-1"), "utf-8");
        Cookie cookie = new Cookie("username", username);

        resp.addCookie(cookie);
//        Cookie[] cookies = req.getCookies();
// 将cookie设置为请求属性
//        req.setAttribute("username", username);
        cookie.setPath("/day32");
       req.getRequestDispatcher("/servlet1.action").forward(req,resp);
        //资源跳转
//        resp.sendRedirect(req.getContextPath() + "/servlet1.action");
    }
}
